package com.sam.im.heiqun.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.adapters.GiftListAdapter;
import com.sam.im.heiqun.uis.beans.GiftBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义Dialog
 *
 * @author linus
 */
public class MyDialog {

    /**
     * 带有计时器的单选dialog
     *
     * @param mContext
     * 上下文
     * @param titleicon
     * 头部图标
     * @param title
     * 头部文本
     * @param message
     * dialog文本
     * @param positiveButton
     * 按钮名称
     * @param time
     * 倒计时时间值
     * @param positiveListener
     * 回调接口
     * @return
     */
//	public static Dialog showTimeAlertDialog(Context mContext, int titleicon,
//			String title, final String message, String positiveButton,
//			final int time, final OnTimeAlertViewClickListener positiveListener) {
//		dialog = new Dialog(mContext, R.style.alertView);
//		timeCount = time;
//		timemessage = message;
//		LayoutInflater inflater = LayoutInflater.from(mContext);
//		View view = (View) inflater.inflate(R.layout.alertview, null);
//
//		Button pButton = new Button(mContext);
//		pButton.setLayoutParams(new LayoutParams(
//				LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//		pButton.setTextColor(mContext.getResources().getColor(
//				R.color.dialog_orange));
//		pButton.setText(positiveButton);
//		pButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (null != positiveListener) {
//					positiveListener.confirm();
//				}
//				dialog.dismiss();
//			}
//		});
//		dialog.setOnDismissListener(new OnDismissListener() {
//			@Override
//			public void onDismiss(DialogInterface dialog) {
//				handler.removeMessages(0);
//				positiveListener.confirm();
//			}
//		});
//		// 动态添加按钮
//		LinearLayout buttonLayout = (LinearLayout) view
//				.findViewById(R.id.buttonLayout);
//		// 一个按钮
//		buttonLayout.setOrientation(LinearLayout.VERTICAL);
//		pButton.setBackgroundResource(R.drawable.alert_time_bottom_button);
//		buttonLayout.addView(pButton);
//		final int viewWidth = dip2px(mContext, 300);
//		view.setMinimumWidth(viewWidth);
//		dialog.setCanceledOnTouchOutside(false);
//		dialog.setContentView(view);
//		dialog.show();
//		handler.sendEmptyMessage(-1);
//		return dialog;
//	}

    private static TextView messageTextView;
    private static Dialog dialog;
    private static int timeCount;// 倒计时时间
    private static String timemessage;// 倒计时信息
    static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            if (what == 0) {
                if (timeCount > 0) {
                    timeCount--;
                    messageTextView.setText(timeCount + timemessage);
                    handler.sendEmptyMessageDelayed(0, 1000);
                } else {
                    dialog.dismiss();
                    handler.removeMessages(0);
                }
            } else {
                handler.sendEmptyMessageDelayed(0, 1000);
            }
            super.handleMessage(msg);
        }
    };


    /**
     * 仅用于展示的弹窗
     *
     * @param mContext
     *            上下文
     * @param sectonalizationTitle
     *            主标题文字
     * @param message
     *            副标题文字（代码中未实现）
     * @param positiveButton
     *            默认最左、上边按钮的文本
     * @param otherButtons
     *            其他的按钮（1个其余按钮，左右分布，2个及以上其余按钮上下分布）的文本
     * @param positiveListener
     *            单击事件监听器，可以设置每一个按钮的单击事件cancel()回调是默认的按钮，confirm()
     *            回调通过上一个参数给定的名称进行索引设置
     * @return
     */
//	public static Dialog showMessageAlertView(Context mContext,
//			String sectonalizationTitle, String message, String positiveButton,
//			final String[] otherButtons,
//			final OnAlertViewClickListener positiveListener) {
//		final Dialog dialog = new Dialog(mContext, R.style.alertView);
//
//		LayoutInflater inflater = LayoutInflater.from(mContext);
//
//		View view = (View) inflater.inflate(R.layout.alertview, null);
//
//		Button pButton = new Button(mContext);
//
//		TextView sectonalization_title = (TextView) view
//				.findViewById(R.id.sectonalization_title);
//		sectonalization_title.setText(sectonalizationTitle);
//
//		// 副标题隐藏
//		View sectonalization_title_sub = view
//				.findViewById(R.id.sectonalization_title_sub);
//		sectonalization_title_sub.setVisibility(View.GONE);
//
//		// 文本框隐藏
//		final EditText inputGroupName = (EditText) view
//				.findViewById(R.id.inputGroupName);
//		inputGroupName.setVisibility(View.GONE);
//
//		pButton.setLayoutParams(new LayoutParams(
//				LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//		pButton.setTextColor(mContext.getResources().getColor(
//				R.color.dialogTxtColor));
//
//		// 动态添加按钮
//		LinearLayout buttonLayout = (LinearLayout) view
//				.findViewById(R.id.buttonLayout);
//
//		if (null == otherButtons || otherButtons.length == 0) {
//			// 一个按钮
//			buttonLayout.setOrientation(LinearLayout.VERTICAL);
//			pButton.setBackgroundResource(R.drawable.alert_bottom_button);
//		} else if (null != otherButtons && otherButtons.length == 1) {
//			// 两个按钮
//			buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
//			pButton.setBackgroundResource(R.drawable.alert_left_button);
//		} else {
//			// 三个或三个以上按钮
//			buttonLayout.setOrientation(LinearLayout.VERTICAL);
//			pButton.setBackgroundResource(R.drawable.alert_middle_button);
//		}
//
//		pButton.setText(positiveButton);
//		pButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (null != positiveListener) {
//
//					positiveListener.cancel();
//				}
//				dialog.dismiss();
//			}
//		});
//
//		buttonLayout.addView(pButton);
//
//		// 将每一个按钮添加进View
//		if (null != otherButtons && otherButtons.length > 0) {
//			for (int i = 0; i < otherButtons.length; i++) {
//				final int index = i;
//				Button otherButton = new Button(mContext);
//				otherButton.setLayoutParams(new LayoutParams(
//						LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//				otherButton.setText(otherButtons[i]);
//
//				otherButton.setTextColor(mContext.getResources().getColor(
//						R.color.dialogTxtColor));
//				if (1 == otherButtons.length) {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_right_button);
//				} else if (i < (otherButtons.length - 1)) {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_middle_button);
//				} else {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_bottom_button);
//				}
//
//				// 设置每一个按钮的单击事件
//				otherButton.setOnClickListener(new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						if (null != positiveListener) {
//							positiveListener.confirm(otherButtons[index],
//									inputGroupName.getText().toString());
//						}
//						dialog.dismiss();
//					}
//				});
//				buttonLayout.addView(otherButton);
//			}
//		}
//
//		final int viewWidth = dip2px(mContext, 300);
//		view.setMinimumWidth(viewWidth);
//		dialog.setCanceledOnTouchOutside(false);
//		dialog.setContentView(view);
//		dialog.show();
//		return dialog;
//	}

    /**
     * 仅用于展示的弹窗
     *
     * @param mContext
     *            上下文
     * @return
     */
//	public static Dialog showNoticeAlertView(Context mContext,
//			String titleStr, String titleStrSub,
//			final Handler mHandler) {
//		final Dialog dialog = new Dialog(mContext, R.style.alertView);
//
//		LayoutInflater inflater = LayoutInflater.from(mContext);
//
//		View view = (View) inflater.inflate(R.layout.alertview_notice, null);
//
////titleLayout_notice
//		TextView titleNotice = (TextView) view
//				.findViewById(R.id.sectonalization_title_noice);
//		titleNotice.setText(titleStr);
//		TextView titleNoticeSub = (TextView) view
//				.findViewById(R.id.sectonalization_title_sub_notice);
//		titleNoticeSub.setText(titleStrSub);
//		ImageView imageClean = (ImageView) view
//				.findViewById(R.id.image_clean_notice);
//		imageClean.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				dialog.dismiss();
//			}
//		});
//		RelativeLayout mRelative = (RelativeLayout) view
//				.findViewById(R.id.titleLayout_notice);
//		mRelative.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				mHandler.sendEmptyMessage(10);
//			}
//		});
//
//		final int viewWidth = dip2px(mContext, 300);
//		view.setMinimumWidth(viewWidth);
//		dialog.setCanceledOnTouchOutside(false);
//		dialog.setContentView(view);
//		dialog.show();
//		return dialog;
//	}

    /**
     * 主标题，副标题，输入文本框，按钮若干
     *
     * @param mContext
     *            上下文
     * @param sectonalizationTitle
     *            主题标题文字
     * @param subTitle
     *            副标题文字
     * @param editTextHint
     *            文本框hint
     * @param positiveButton
     *            默认的按钮（最左边）文本
     * @param otherButtons
     *            其他按钮文本，数组
     * @param
     * @return
     */
//	public static Dialog showAlertView(Context mContext,
//			String sectonalizationTitle, String subTitle, String editTextHint,
//			String positiveButton, final String[] otherButtons,
//			final OnAlertViewClickListener positiveListener) {
//		final Dialog dialog = new Dialog(mContext, R.style.alertView);
//
//		LayoutInflater inflater = LayoutInflater.from(mContext);
//
//		// 弹出的主view
//		View view = (View) inflater.inflate(R.layout.alertview, null);
//
//		// 主标题
//		TextView sectonalization_title = (TextView) view
//				.findViewById(R.id.sectonalization_title);
//		sectonalization_title.setText(sectonalizationTitle);
//
//		// 副标题
//		TextView sectonalization_title_sub = (TextView) view
//				.findViewById(R.id.sectonalization_title_sub);
//		sectonalization_title_sub.setText(subTitle);
//
//		// 文本框
//		final EditText inputGroupName = (EditText) view
//				.findViewById(R.id.inputGroupName);
//		inputGroupName.setHint(editTextHint);
//
//		Button pButton = new Button(mContext);
//		pButton.setLayoutParams(new LayoutParams(
//				LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//		pButton.setTextColor(mContext.getResources().getColor(
//				R.color.dialogTxtColor));
//
//		// 动态添加按钮
//		LinearLayout buttonLayout = (LinearLayout) view
//				.findViewById(R.id.buttonLayout);
//		if (null == otherButtons || otherButtons.length == 0) {
//			// 一个按钮
//			buttonLayout.setOrientation(LinearLayout.VERTICAL);
//			pButton.setBackgroundResource(R.drawable.alert_bottom_button);
//		} else if (null != otherButtons && otherButtons.length == 1) {
//			// 两个按钮
//			buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
//			pButton.setBackgroundResource(R.drawable.alert_left_button);
//		} else {
//			// 三个或三个以上按钮
//			buttonLayout.setOrientation(LinearLayout.VERTICAL);
//			pButton.setBackgroundResource(R.drawable.alert_middle_button);
//		}
//
//		pButton.setText(positiveButton);
//		pButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (null != positiveListener) {
//					positiveListener.cancel();
//				}
//				dialog.dismiss();
//			}
//		});
//		buttonLayout.addView(pButton);
//		if (null != otherButtons && otherButtons.length > 0) {
//			for (int i = 0; i < otherButtons.length; i++) {
//				final int index = i;
//				Button otherButton = new Button(mContext);
//				otherButton.setLayoutParams(new LayoutParams(
//						LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//				otherButton.setText(otherButtons[i]);
//
//				otherButton.setTextColor(mContext.getResources().getColor(
//						R.color.dialogTxtColor));
//				if (1 == otherButtons.length) {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_right_button);
//				} else if (i < (otherButtons.length - 1)) {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_middle_button);
//				} else {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_bottom_button);
//				}
//				otherButton.setOnClickListener(new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						if (null != positiveListener) {
//							positiveListener.confirm(otherButtons[index],
//									inputGroupName.getText().toString());
//						}
//						dialog.dismiss();
//					}
//				});
//				buttonLayout.addView(otherButton);
//			}
//		}
//		final int viewWidth = dip2px(mContext, 300);
//		view.setMinimumWidth(viewWidth);
//		dialog.setCanceledOnTouchOutside(false);
//		dialog.setContentView(view);
//		dialog.show();
//		return dialog;
//	}

    /**
     * 实现一个按钮，2个按钮
     *
     * @param mContext
     *            上下文
     * @param titleicon
     *            头部图标
     * @param title
     *            头部文字
     * @param message
     *            内容
     * @param positiveButton
     *            一个默认按钮
     * @param otherButtons
     *            其他按钮
     * @param positiveListener
     *            回调监听
     * @return
     */
//	public static Dialog showAlertView(Context mContext, int titleicon,
//			String title, String message, String positiveButton,
//			final String[] otherButtons,
//			final OnAlertViewClickNormalListener positiveListener) {
//		final Dialog dialog = new Dialog(mContext, R.style.alertView);
//
//		LayoutInflater inflater = LayoutInflater.from(mContext);
//		View view = (View) inflater.inflate(R.layout.alertview2, null);
//		// 标题和内容
//		TextView titleTextView = (TextView) view
//				.findViewById(R.id.titleTextView);
//		TextView messageTextView = (TextView) view
//				.findViewById(R.id.messageTextView);
//		ImageView icon = (ImageView) view.findViewById(R.id.titileicon);
//		if (0 != titleicon) {
//			icon.setBackgroundResource(titleicon);
//		} else {
//			icon.setVisibility(View.GONE);
//		}
//		if (null != title && !title.equalsIgnoreCase("")) {
//			titleTextView.setText(title);
//		} else {
//			titleTextView.setVisibility(View.GONE);
//		}
//		if (null != message && !message.equalsIgnoreCase("")) {
//			messageTextView.setText(message);
//		} else {
//			messageTextView.setVisibility(View.GONE);
//		}
//		Button pButton = new Button(mContext);
//		pButton.setLayoutParams(new LayoutParams(
//				LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//		pButton.setTextColor(mContext.getResources().getColor(
//				R.color.dialogTxtColor));
//		// 动态添加按钮
//		LinearLayout buttonLayout = (LinearLayout) view
//				.findViewById(R.id.buttonLayout);
//		if (null == otherButtons || otherButtons.length == 0) {
//			// 一个按钮
//			buttonLayout.setOrientation(LinearLayout.VERTICAL);
//			pButton.setBackgroundResource(R.drawable.alert_bottom_button);
//		} else if (null != otherButtons && otherButtons.length == 1) {
//			// 两个按钮
//			buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
//			pButton.setBackgroundResource(R.drawable.alert_left_button);
//		} else {
//			// 三个或三个以上按钮
//			buttonLayout.setOrientation(LinearLayout.VERTICAL);
//			pButton.setBackgroundResource(R.drawable.alert_middle_button);
//		}
//
//		pButton.setText(positiveButton);
//		pButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (null != positiveListener) {
//					positiveListener.cancel();
//				}
//				dialog.dismiss();
//			}
//		});
//		buttonLayout.addView(pButton);
//		if (null != otherButtons && otherButtons.length > 0) {
//			for (int i = 0; i < otherButtons.length; i++) {
//				final int index = i;
//				Button otherButton = new Button(mContext);
//				otherButton.setLayoutParams(new LayoutParams(
//						LayoutParams.MATCH_PARENT, dip2px(mContext, 44), 1.0f));
//				otherButton.setText(otherButtons[i]);
//				otherButton.setTextColor(mContext.getResources().getColor(
//						R.color.dialogTxtColor));
//				if (1 == otherButtons.length) {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_right_button);
//				} else if (i < (otherButtons.length - 1)) {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_middle_button);
//				} else {
//					otherButton
//							.setBackgroundResource(R.drawable.alert_bottom_button);
//				}
//				otherButton.setOnClickListener(new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						if (null != positiveListener) {
//							positiveListener.confirm(otherButtons[index]);
//						}
//						dialog.dismiss();
//					}
//				});
//				buttonLayout.addView(otherButton);
//			}
//		}
//		final int viewWidth = dip2px(mContext, 250);
//		view.setMinimumWidth(viewWidth);
//		dialog.setCanceledOnTouchOutside(false);
//		dialog.setContentView(view);
//		dialog.show();
//		return dialog;
//	}

    /**
     * 底部弹出dialog
     *
     * @param mContext
     * @param title
     * @param items
     * @param dialogClickListener
     * @return
     */
    public static Dialog ShowDialog(Context mContext, String title,
                                    String[] items, final DialogItemClickListener dialogClickListener) {
        final Dialog dialog = new Dialog(mContext,
                R.style.DialogStyle);

        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_radio, null);
        dialog.setContentView(view);
        if (title == null || title.equals("")) {
            ((TextView) view.findViewById(R.id.title)).setVisibility(View.GONE);
            ((TextView) view.findViewById(R.id.title_divider))
                    .setVisibility(View.GONE);
        } else {
            ((TextView) view.findViewById(R.id.title)).setText(title);
        }
        // 根据items动态创建
        LinearLayout parent = (LinearLayout) view
                .findViewById(R.id.dialogLayout);
        parent.removeAllViews();
        int length = items.length;
        for (int i = 0; i < length; i++) {
            LayoutParams params1 = new LayoutParams(-1, -2);
            params1.rightMargin = 1;
            final TextView tv = new TextView(mContext);
            tv.setLayoutParams(params1);
            tv.setTextSize(18);
            tv.setText(items[i]);
            tv.setTextColor(mContext.getResources().getColor(
                    R.color.text_color_gray1));
            int pad = mContext.getResources().getDimensionPixelSize(
                    R.dimen.spacing_default);
            tv.setPadding(pad, pad, pad, pad);
            tv.setSingleLine(true);
            tv.setGravity(Gravity.CENTER);

            if (i != length - 1) {
                if ((title.equals("") || title == null) && i == 0) {
                    tv.setBackgroundResource(R.drawable.menudialog_top2_selector);

                } else {
                    tv.setBackgroundResource(R.drawable.menudialog_center_selector);
                }
            } else {
                if (length == 1 && i != 0) {
                    tv.setBackgroundResource(R.drawable.menudialog_bottom_selector);
                } else {
                    tv.setBackgroundResource(R.drawable.menudialog_bottom2_selector);
                }
                if (length == 1 && i == 0) {
                    tv.setBackgroundResource(R.drawable.menudialog_bottom3_selector);
                }
            }
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    dialog.dismiss();
                    dialogClickListener.confirm(tv.getText().toString());
                }
            });
            parent.addView(tv);
            if (i != length - 1) {
                TextView divider = new TextView(mContext);
                LayoutParams params = new LayoutParams(-1, (int) 1);
                divider.setLayoutParams(params);
                divider.setBackgroundResource(android.R.color.darker_gray);
                parent.addView(divider);
            }
        }
        view.findViewById(R.id.ok).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth(mContext);

        // 贴屏幕底部
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
//        dialog.show();
        return dialog;
    }


    /**
     * 底部弹出dialog
     *
     * 朋友圈礼物列表
     *
     * @param mContext
     * @param dialogClickListener
     * @return
     */
    public static Dialog ShowGiftDialog(Context mContext, final DialogItemClickListener dialogClickListener) {
        final Dialog dialog = new Dialog(mContext,
                R.style.Translucent_NoTitle);

        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_gift_list, null);
        dialog.setContentView(view);

        GridView grid_gift = (GridView) view.findViewById(R.id.grid_gift);
        Button but_send = (Button) view.findViewById(R.id.but_send);

        List<GiftBean> giftBeanList = new ArrayList<>();
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01", R.drawable.gift_001, true));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));
        giftBeanList.add(new GiftBean("玫瑰", "gift_001", "0.01",  R.drawable.gift_001, false));

        final GiftListAdapter adapter = new GiftListAdapter(giftBeanList, mContext);
        grid_gift.setAdapter(adapter);
        grid_gift.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null!= adapter.getData() && position < adapter.getData().size()){
                    adapter.refresh(position);
                }
            }
        });
        but_send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                List<GiftBean> giftBeanLista = adapter.getData() ;
                GiftBean giftBean = null ;
                for (int i = 0; i < giftBeanLista.size(); i++) {
                    if (giftBeanLista.get(i).isSelecte()){
                        giftBean = giftBeanLista.get(i);
                    }
                }
                if (null!=giftBean){
                    String h = "{\"name\":\""+giftBean.getName()+"\",\"key\":\""+giftBean.getKey()+"\",\"price\":\""+giftBean.getPrice()+"\"}";
                    dialogClickListener.confirm(h);
                    dialog.dismiss();
                }

            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth(mContext);

        // 贴屏幕底部
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return dialog;
    }
    /**
     * 底部弹出dialog
     *
     * @param mContext
     * @param dialogClickListener
     * @return
     */
    public static Dialog ShowAudioTxtDialog(Context mContext, final DialogAudioClickListener dialogClickListener) {
        final Dialog dialog = new Dialog(mContext,
                R.style.DialogStyle);

        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_audio_txt, null);
        dialog.setContentView(view);

        final TextView title = (TextView) view.findViewById(R.id.title);
        TextView ok = (TextView) view.findViewById(R.id.txt_ok);
        TextView clean = (TextView) view.findViewById(R.id.txt_clean);
        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogClickListener.onok(title.getText().toString());
                dialog.dismiss();
            }
        });//RecordVideo
        clean.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ImageView image_button = (ImageView) view.findViewById(R.id.image_button);
        image_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int startX = 0 ;
                int startY = 0 ;
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.i("chatbase", "onTouch: ==ACTION_DOWN==");
                        startX = (int)event.getX();
                        startY = (int)event.getY();
                        dialogClickListener.onstart();
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i("chatbase", "onTouch: ==ACTION_UP==");
                        dialogClickListener.onend(title);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.i("chatbase", "onTouch: ==ACTION_MOVE==");
                        int thisX = (int)event.getX();
                        int thisY = (int)event.getY();

                        if (thisX - startX > 200 || thisX - startX < -200
                                || thisY - startY > 200 || thisY - startY < -200){
                            dialogClickListener.onend(title);
                        }
                        break;
                }
                return true;
            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth(mContext);

        // 贴屏幕底部
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return dialog;
    }

    public static void showImgDialog(final Activity activity, final Uri imageUri) {
        MyDialog.ShowDialog(activity, "", new String[]{activity.getString(R.string.photograph), activity.getString(R.string.from_album)}, new MyDialog.DialogItemClickListener() {
            @Override
            public void confirm(String result) {
                if (activity.getResources().getString(R.string.photograph).equals(result)) {
                    final String state = Environment.getExternalStorageState();
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        activity.startActivityForResult(captureIntent, 66);
                    }
                } else if (activity.getResources().getString(R.string.from_album).equals(result)) {
                    Intent intent = new Intent();
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("image/*");
                    intent.putExtra("crop", "true");
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    intent.putExtra("outputX", 80);
                    intent.putExtra("outputY", 80);
                    intent.putExtra("return-data", true);
                    //根据版本号不同使用不同的Action
                    if (Build.VERSION.SDK_INT < 19) {
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                    } else {
                        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    }
                    activity.startActivityForResult(intent, 77);
                }
            }
        });
    }

    public static void showImgDialog(final Activity activity, final Fragment fragment, final Uri imageUri) {
        MyDialog.ShowDialog(activity, "", new String[]{activity.getString(R.string.photograph), activity.getString(R.string.from_album)}, new MyDialog.DialogItemClickListener() {
            @Override
            public void confirm(String result) {
                if (activity.getResources().getString(R.string.photograph).equals(result)) {

                    final String state = Environment.getExternalStorageState();
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        activity.startActivityForResult(captureIntent, 66);
                    }
                } else if (activity.getResources().getString(R.string.from_album).equals(result)) {
                    Intent intent = new Intent();
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("image/*");
                    intent.putExtra("crop", "true");
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    intent.putExtra("outputX", 80);
                    intent.putExtra("outputY", 80);
                    intent.putExtra("return-data", true);
                    //根据版本号不同使用不同的Action
                    if (Build.VERSION.SDK_INT < 19) {
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                    } else {
                        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    }
                    activity.startActivityForResult(intent, 77);
                }
            }
        });
    }
    public static void showBGDialog(final Activity activity,final Handler handler) {

        MyDialog.ShowDialog(activity, "", new String[]{activity.getString(R.string.title_bg_tk), activity.getString(R.string.title_bg_mr)}, new MyDialog.DialogItemClickListener() {
            @Override
            public void confirm(String result) {
                if (activity.getResources().getString(R.string.title_bg_tk).equals(result)) {
                    handler.sendEmptyMessage(106);
                } else if (activity.getResources().getString(R.string.title_bg_mr).equals(result)) {
                    handler.sendEmptyMessage(107);
                }
            }
        }).show();
    }

    /**
     * 显示一个正在加载的对话框
     *
     * @param context       环境上下文
     * @param title         主标题
     * @param msg           正文
     * @param setCancelable 用户是否可以手动关闭
     * @return
     */
    public static ProgressDialog showLoadingDialog(Context context,
                                                   String title, String msg, boolean setCancelable) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setCancelable(setCancelable);
        dialog.setCanceledOnTouchOutside(setCancelable);
        dialog.show();
        return dialog;
    }

    /**
     * 回调接口
     *
     * @author Administrator
     */
    public interface OnAlertViewClickListener {

        /**
         * 其余按钮单击事件
         *
         * @param result     字符索引
         * @param inputGroup 用户在文本框中输入
         */
        void confirm(String result, String inputGroup);

        void cancel();
    }

    /**
     * 回调接口
     *
     * @author Administrator
     */
    public interface OnAlertViewClickNormalListener {
        /**
         * 索引字符串，确定是哪个按钮被点击，值为传入时显示在按钮上的文字
         *
         * @param indexStr 字符索引
         */
        public abstract void confirm(String indexStr);

        /**
         * 默认的主按钮点击事件
         */
        public abstract void cancel();
    }

    /**
     * 时间dialog回调接口
     *
     * @author Administrator
     */
    public interface OnTimeAlertViewClickListener {
        public abstract void confirm();
    }

    public interface DialogItemClickListener {
        void confirm(String result);
    }

    public interface DialogAudioClickListener {
        public abstract void onstart();
        public abstract void onend(TextView title);
        public abstract void onok(String result);
    }

    /**
     * 获取屏幕分辨率宽
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 获取屏幕分辨率高
     */
    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
        return dm.heightPixels;
    }

    /**
     * 获取屏幕分辨率宽计算dialog的宽度
     */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}
